package cn;

import cn.webrx.entity.Student;
import cn.webrx.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-09-15 16:22
 */
public class Demo {
    protected SqlSession ss;
    protected SqlSessionFactory sf;
    protected StudentMapper studao;


    @Test
    public void m2m(){

        //会话一
        UsersMapper usdao = ss.getMapper(UsersMapper.class);
        System.out.println(usdao.queryById(2));
        ss.commit();//将当前操作一级缓存，支持二级 添加到二级缓存
        //ss.clearCache();

        //会话二  二级缓存
        UsersMapper us = sf.openSession().getMapper(UsersMapper.class);
        System.out.println(us.queryById(2));
        //ss.clearCache();
        //System.out.println(usdao.queryById(2));

    }


    @Test
    public void onetoone(){
        MemberInfoMapper midao = ss.getMapper(MemberInfoMapper.class);
        //System.out.println(midao.queryByMemberId(1));

        MemberMapper mdao = ss.getMapper(MemberMapper.class);
        //System.out.println(mdao.queryById(1));

        //List<Member> ms = mdao.queryAll();
        //System.out.println(ms.get(0).getMinfo());

        System.out.println(mdao.queryAll());
    }



    @Test
    public void ttbook() {
        var bookdao = ss.getMapper(BookMapper.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("price", 38.5);
        map.put("pdate", LocalDate.of(2020, 3, 15));
        map.put("id", 34);
        map.clear();
        map.put("priceinc", 15);
        map.put("id", 34);
        //bookdao.update(map);


        map.clear();
        map.put("id", 100);
        map.put("name", "《java实战》");
        map.clear();
        map.put("ids", new int[]{1, 20, 30, 70, 50});
        //bookdao.delete(map);


        map.clear();
        map.put("id",60);
        map.put("name","aaaa");
        map.clear();
        bookdao.show(map);
    }


    @Test
    public void testsql() {
        UserBookMapper udao = ss.getMapper(UserBookMapper.class);
        //System.out.println(udao.query());
        //System.out.println(udao.query("《java实战》"));
        //System.out.println(udao.query("%java%"));

        //System.out.println(udao.wh(null));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("gteprice", 80.0);
        map.put("name", "《数据库实战》");
        map.clear();
        map.put("gteprice", 30);
        map.put("order", "price");

        map.clear();


        System.out.println(udao.wh(map));
    }


    @Test
    public void testuserbook() {
        UserBookMapper udao = ss.getMapper(UserBookMapper.class);
        System.out.println(udao.show());
    }


    @Test
    public void testbook() {
        BookMapper bookdao = ss.getMapper(BookMapper.class);
        System.out.println(bookdao.queryAll());
    }


    @Test
    public void testmapper() {


        NewsMapper newsdao = ss.getMapper(NewsMapper.class);
        //News news = new News();
        //news.setId(0);
        //news.setTitle("php");
        //news.setContent("hello world");
        //newsdao.insert(news);

        System.out.println(newsdao.selectAll());


    }


    @Before
    public void init() {
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            sf = new SqlSessionFactoryBuilder().build(is);

            MapperHelper mh = new MapperHelper();
            mh.registerMapper(Mapper.class);
            mh.processConfiguration(sf.getConfiguration());

            ss = sf.openSession();
            studao = ss.getMapper(StudentMapper.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @After
    public void close() {
        ss.commit();
        ss.close();
    }

    @Test
    public void page() {
        int currpage = 1;
        int pagesize = 2;
        RowBounds rows = new RowBounds(currpage * pagesize - pagesize, pagesize);
        System.out.println(studao.queryNamePage(rows));
        System.out.println("-".repeat(100));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("index", currpage * pagesize - pagesize);
        map.put("pagesize", pagesize);
        System.out.println(studao.queryNamePages(map));
        System.out.println("-".repeat(100));

        PageHelper.startPage(currpage, pagesize);
        List<String> list = studao.queryNames();

        PageInfo<String> pi = new PageInfo<>(list);
        System.out.println(pi.getTotal());
        System.out.println(pi.getPageSize());
        System.out.println(pi.getPageNum());
        System.out.println(pi.getPages());

        for (String n : pi.getList()) {
            System.out.println(n);
        }

    }


    @Test
    public void query() {
        System.out.println(studao.count());

        for (String t : studao.showTables()) {
            System.out.println(t);
        }
    }

    @Test
    public void del() {
        int i = studao.deleteById(3);
        System.out.println(i);

        studao.deleteByName("李%");
    }

    @Test
    public void update() {
        //studao.update(2, 88, 38);
        Student st = studao.queryById(2);
        st.setAge(100);
        studao.updates(st);
    }

    @Test
    public void select() {
        //Student st = studao.queryById(2);
        //System.out.println(st);

        //System.out.println(studao.queryAll());
        //System.out.println(studao.queryMy());

        System.out.println(studao.queryByIds(2200));


        System.out.println(studao.queryByNameSex("%", "女"));


    }

    @Test
    public void add() {
        Student st = new Student();
        st.setAddress("上海市");
        st.setAge(19);
        st.setBirthday(LocalDate.of(1995, 4, 15));
        st.setInfo("是学生");
        st.setSex("女");
        st.setName("李梅梅");
        st.setMoney(9000);
        st.setDept("计算机科学");
        st.setScore(98);
        st.setRegtime(LocalDateTime.now());
        //studao.save(st);

        //studao.add("李李李",50,20,"男");

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "李1234");
        map.put("score", 90);
        map.put("age", 19);
        map.put("sex", "男");
        studao.insert(map);

        System.out.println(map.get("id"));
    }
}
